#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"

#define DBNAME "/tmp/sqltest.db"
#define CREATE "create table %s (id INTEGER primary key AUTOINCREMENT, name text, age int);"
int main(void)
{
	printf("Content-Type:text/html;\n\n");

	sqlite3 *ppdb = NULL;
	int ret = sqlite3_open(DBNAME, &ppdb);
	if(ret != SQLITE_OK)
	{
		printf("<p>open fail\n\n");
		return -1;
	}
	
	//获取httpget请求参数
	char *data = getenv("QUERY_STRING");
	if(data == NULL)
	{
				printf("get error\n\n");
	}

	//表名
	char tablename[32]={0};
	sscanf(data, "table_name=%s", tablename);//解析表名
	char sql[128]={0};
	sprintf(sql, CREATE, tablename);//打开sql语句

	char *err=NULL;
	ret = sqlite3_exec(ppdb, sql, NULL, NULL, &err);//创建表格
	if(ret != SQLITE_OK)
	{
		printf("<p>%s\n\n", err);
	}

	printf("<p>create suc\n\n");	
	
	sqlite3_close(ppdb);
	return 0;
}